#include "expressionWidget.h"
#include "cubeFeature.h"
#include "modelSoft.h"
#include <qmessagebox.h>
expressionWidget::expressionWidget(modelSoft* myModelSoft,QWidget *parent)
	: myModelSoft(myModelSoft)
	, QWidget(parent)
	, ui(new Ui::expressionWidgetClass())
{
	ui->setupUi(this);
	ui->expressionSubWidget->verticalHeader()->setVisible(false);
	ui->modifyButton->setEnabled(false);
	ui->confirmButton->setEnabled(false);
	//添加已有表达式
	for (std::map<Standard_Integer, BaseFeature*>::iterator iter = myModelSoft->m_Features.begin(); iter != myModelSoft->m_Features.end(); ++iter) {
		BaseFeature* feature = iter->second;
		addExpression(feature);
	}
	createConnection();
}

expressionWidget::~expressionWidget()
{
	delete ui;
}

void expressionWidget::addExpression(BaseFeature* feature)
{
	QTableWidget* table = ui->expressionSubWidget;
	int row = table->rowCount();
	int type = feature->getFeatureType();
	/*QTableWidgetItem* item1;
	QTableWidgetItem* item2;
	QTableWidgetItem* item3;
	QTableWidgetItem* item4;*/
	switch (type) 
	{
	case 1:
	{
		CubeFeature* cubeFeature = (CubeFeature*)feature;
		QTableWidgetItem* item1 = new QTableWidgetItem(QString("length%1").arg(cubeFeature->getShapeId()));
		QTableWidgetItem* item2 = new QTableWidgetItem(QString("%1").arg(cubeFeature->getLength()));
		QTableWidgetItem* item3 = new QTableWidgetItem(QString("%1").arg(cubeFeature->getLength()));
		QTableWidgetItem* item4 = new QTableWidgetItem(QString("长方体%1-length").arg(cubeFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item1);
		table->setItem(row, 1, item2);
		table->setItem(row, 2, item3);
		table->setItem(row, 3, item4);
		row++;
		QTableWidgetItem* item5 = new QTableWidgetItem(QString("width%1").arg(cubeFeature->getShapeId()));
		QTableWidgetItem* item6 = new QTableWidgetItem(QString("%1").arg(cubeFeature->getWidth()));
		QTableWidgetItem* item7 = new QTableWidgetItem(QString("%1").arg(cubeFeature->getWidth()));
		QTableWidgetItem* item8 = new QTableWidgetItem(QString("长方体%1-width").arg(cubeFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item5);
		table->setItem(row, 1, item6);
		table->setItem(row, 2, item7);
		table->setItem(row, 3, item8);
		row++;
		QTableWidgetItem* item9 = new QTableWidgetItem(QString("high%1").arg(cubeFeature->getShapeId()));
		QTableWidgetItem* item0 = new QTableWidgetItem(QString("%1").arg(cubeFeature->getHigh()));
		QTableWidgetItem* item11 = new QTableWidgetItem(QString("%1").arg(cubeFeature->getHigh()));
		QTableWidgetItem* item12 = new QTableWidgetItem(QString("长方体%1-high").arg(cubeFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item9);
		table->setItem(row, 1, item0);
		table->setItem(row, 2, item11);
		table->setItem(row, 3, item12);
		row++;
		break;
	}
	case 2:
	{
		ConeFeature* coneFeature = (ConeFeature*)feature;
		QTableWidgetItem* item1 = new QTableWidgetItem(QString("radius%1").arg(coneFeature->getShapeId()));
		QTableWidgetItem* item2 = new QTableWidgetItem(QString("%1").arg(coneFeature->getRadius1()));
		QTableWidgetItem* item3 = new QTableWidgetItem(QString("%1").arg(coneFeature->getRadius1()));
		QTableWidgetItem* item4 = new QTableWidgetItem(QString("圆锥体%1-radius1").arg(coneFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item1);
		table->setItem(row, 1, item2);
		table->setItem(row, 2, item3);
		table->setItem(row, 3, item4);
		row++;
		QTableWidgetItem* item5 = new QTableWidgetItem(QString("radius%1").arg(coneFeature->getShapeId()));
		QTableWidgetItem* item6 = new QTableWidgetItem(QString("%1").arg(coneFeature->getRadius2()));
		QTableWidgetItem* item7 = new QTableWidgetItem(QString("%1").arg(coneFeature->getRadius2()));
		QTableWidgetItem* item8 = new QTableWidgetItem(QString("圆锥体%1-radius2").arg(coneFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item5);
		table->setItem(row, 1, item6);
		table->setItem(row, 2, item7);
		table->setItem(row, 3, item8);
		row++;
		QTableWidgetItem* item9 = new QTableWidgetItem(QString("high%1").arg(coneFeature->getShapeId()));
		QTableWidgetItem* item0 = new QTableWidgetItem(QString("%1").arg(coneFeature->getHigh()));
		QTableWidgetItem* item11 = new QTableWidgetItem(QString("%1").arg(coneFeature->getHigh()));
		QTableWidgetItem* item12 = new QTableWidgetItem(QString("圆锥体%1-high").arg(coneFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item9);
		table->setItem(row, 1, item0);
		table->setItem(row, 2, item11);
		table->setItem(row, 3, item12);
		row++;
		break;
	}
	case 3:
	{
		SphereFeature* sphereFeature = (SphereFeature*)feature;
		QTableWidgetItem* item1 = new QTableWidgetItem(QString("radius%1").arg(sphereFeature->getShapeId()));
		QTableWidgetItem* item2 = new QTableWidgetItem(QString("%1").arg(sphereFeature->getRadius1()));
		QTableWidgetItem* item3 = new QTableWidgetItem(QString("%1").arg(sphereFeature->getRadius1()));
		QTableWidgetItem* item4 = new QTableWidgetItem(QString("球体%1-radius1").arg(sphereFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item1);
		table->setItem(row, 1, item2);
		table->setItem(row, 2, item3);
		table->setItem(row, 3, item4);
		row++;
		break;
	}
	case 4:
	{
		CylinderFeature* cylinderFeature = (CylinderFeature*)feature;
		QTableWidgetItem* item1 = new QTableWidgetItem(QString("radius%1").arg(cylinderFeature->getShapeId()));
		QTableWidgetItem* item2 = new QTableWidgetItem(QString("%1").arg(cylinderFeature->getRadius1()));
		QTableWidgetItem* item3 = new QTableWidgetItem(QString("%1").arg(cylinderFeature->getRadius1()));
		QTableWidgetItem* item4 = new QTableWidgetItem(QString("圆柱体%1-radius").arg(cylinderFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item1);
		table->setItem(row, 1, item2);
		table->setItem(row, 2, item3);
		table->setItem(row, 3, item4);
		row++;
		QTableWidgetItem* item5 = new QTableWidgetItem(QString("high%1").arg(cylinderFeature->getShapeId()));
		QTableWidgetItem* item6 = new QTableWidgetItem(QString("%1").arg(cylinderFeature->getHigh()));
		QTableWidgetItem* item7 = new QTableWidgetItem(QString("%1").arg(cylinderFeature->getHigh()));
		QTableWidgetItem* item8 = new QTableWidgetItem(QString("圆柱体%1-high").arg(cylinderFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item5);
		table->setItem(row, 1, item6);
		table->setItem(row, 2, item7);
		table->setItem(row, 3, item8);
		row++;
		break;
	}
	case 5:
	{
		TorusFeature* torusFeature = (TorusFeature*)feature;
		QTableWidgetItem* item1 = new QTableWidgetItem(QString("radius%1").arg(torusFeature->getShapeId()));
		QTableWidgetItem* item2 = new QTableWidgetItem(QString("%1").arg(torusFeature->getRadius1()));
		QTableWidgetItem* item3 = new QTableWidgetItem(QString("%1").arg(torusFeature->getRadius1()));
		QTableWidgetItem* item4 = new QTableWidgetItem(QString("圆环体%1-radius1").arg(torusFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item1);
		table->setItem(row, 1, item2);
		table->setItem(row, 2, item3);
		table->setItem(row, 3, item4);
		row++;
		QTableWidgetItem* item5 = new QTableWidgetItem(QString("radius%1").arg(torusFeature->getShapeId()));
		QTableWidgetItem* item6 = new QTableWidgetItem(QString("%1").arg(torusFeature->getRadius2()));
		QTableWidgetItem* item7 = new QTableWidgetItem(QString("%1").arg(torusFeature->getRadius2()));
		QTableWidgetItem* item8 = new QTableWidgetItem(QString("圆环体%1-radius2").arg(torusFeature->getShapeId()));
		table->insertRow(row);
		table->setItem(row, 0, item5);
		table->setItem(row, 1, item6);
		table->setItem(row, 2, item7);
		table->setItem(row, 3, item8);
		row++;
		break;
	}
	}
}

void expressionWidget::updateFeature(BaseFeature* selectedFeature, double value)
{
	QTableWidgetItem* itemExplain = ui->expressionSubWidget->item(selectedRow, 3);
	//获取字符串'-'后的字符串
	QString text = itemExplain->text().right(itemExplain->text().size() - (itemExplain->text().lastIndexOf("-") + 1));
	int featureType = selectedFeature->getFeatureType();
	switch (featureType) {
	case 1://cube
	{
		CubeFeature* feature = (CubeFeature*)selectedFeature;
		if (text == "length") {
			feature->setLength(value);
		}
		else if (text == "width") {
			feature->setWidth(value);
		}
		else {
			feature->setHigh(value);
		}
		break;
	}
	case 5://torus
	{
		TorusFeature* feature = (TorusFeature*)selectedFeature;
		if (text == "radius1") {
			feature->setRadius1(value);
		}
		else {
			feature->setRadius2(value);
		}
		break;
	}
	case 2://cone
	{
		ConeFeature* feature = (ConeFeature*)selectedFeature;
		if (text == "radius1") {
			feature->setRadius1(value);
		}
		else if (text == "radius2") {
			feature->setRadius2(value);
		}
		else {
			feature->setHigh(value);
		}
		break;
	}
	case 3://sphere
	{
		SphereFeature* feature = (SphereFeature*)selectedFeature;
		if (text == "radius1") {
			feature->setRadius1(value);
		}
		break;
	}
	case 4://cylinder
	{
		CylinderFeature* feature = (CylinderFeature*)selectedFeature;
		if (text == "radius1") {
			feature->setRadius1(value);
		}
		else {
			feature->setHigh(value);
		}
	}
	}
}

void expressionWidget::createConnection()
{
	connect(ui->modifyButton, &QPushButton::clicked, this, &expressionWidget::onModify);
	connect(ui->confirmButton, &QPushButton::clicked, this, &expressionWidget::onConfirm);
	connect(ui->cancelButton, &QPushButton::clicked, this, &expressionWidget::close);
	connect(ui->editExpressionButton, &QPushButton::clicked, this, &expressionWidget::onEditExpressionButtonClicked);
	connect(ui->expressionSubWidget, &QTableWidget::itemClicked, this, &expressionWidget::onItemClicked);
}

void expressionWidget::onItemClicked(QTableWidgetItem* item)
{
	//记录选中表达式所在行
	selectedRow = item->row();
	int selectedShapeId = QString(ui->expressionSubWidget->item(selectedRow, 0)->text().back()).toInt();
	//查找id对应的模型Feature
	auto iter = myModelSoft->m_Features.find(selectedShapeId);
	if (iter == myModelSoft->m_Features.end()) {
		return;
	}
	selectedFeature = iter->second;
	QString expressionName = ui->expressionSubWidget->item(selectedRow, 0)->text();
	ui->expressionNameInput->setText(expressionName);
	QString expressionValue = ui->expressionSubWidget->item(selectedRow, 2)->text();
	ui->expressionValueInput->setText(expressionValue);
	lastExpressionValue = expressionValue.toDouble();
}

void expressionWidget::onEditExpressionButtonClicked()
{
	QString expressionName = ui->expressionNameInput->text();
	QString expressionValue = ui->expressionValueInput->text();
	QTableWidgetItem* itemName = ui->expressionSubWidget->item(selectedRow, 0);
	QTableWidgetItem* itemValue = ui->expressionSubWidget->item(selectedRow, 2);
	QTableWidgetItem* item = ui->expressionSubWidget->item(selectedRow, 1);
	itemName->setText(expressionName);
	itemValue->setText(expressionValue);
	item->setText(expressionValue);
	
	//更新特征
	updateFeature(selectedFeature, itemValue->text().toDouble());
	featureModify = true;
	ui->modifyButton->setEnabled(true);
}

void expressionWidget::onModify()
{
	//判断该模型是否被操作过
	std::list<BaseFeature*> list = selectedFeature->GetOperationList();
	if (list.size() == 0) {
		//未操作，直接修改基本体素模型
		myModelSoft->RemovePipelineFromRendererById(selectedFeature->getShapeId());
		myModelSoft->CreatePipelineByFeature(selectedFeature);
		modify = true;
		ui->confirmButton->setEnabled(true);
		return;
	}
	else {
		//撤消重做时已完成递归操作了，只需对最后操作进行撤消重做即可(前面操作会递归的在撤消重做中进行)
		//获取list链尾元素
        BaseFeature* lastFeature = list.back();
		myModelSoft->m_historyManager->undoByFeature(lastFeature);
		myModelSoft->m_historyManager->redoByFeature(lastFeature);
		int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
		myModelSoft->m_historyManager->historyTree->removeRow(row1);
		modify = true;
		ui->confirmButton->setEnabled(true);
	}
	
}

void expressionWidget::closeEvent(QCloseEvent* event)
{
	if (!confirm) {
		if (modify) {
			//还原特征值
			updateFeature(selectedFeature, lastExpressionValue);
			//判断该模型是否被操作过
			std::list<BaseFeature*> list = selectedFeature->GetOperationList();
			if (list.size() == 0) {
				//未操作，直接修改基本体素模型
				myModelSoft->RemovePipelineFromRendererById(selectedFeature->getShapeId());
				myModelSoft->CreatePipelineByFeature(selectedFeature);
				modify = true;
				ui->confirmButton->setEnabled(true);
				return;
			}
			else {
				//撤消重做时已完成递归操作了，只需对最后操作进行撤消重做即可(前面操作会递归的在撤消重做中进行)
				//获取list链尾元素
				BaseFeature* lastFeature = list.back();
				myModelSoft->m_historyManager->undoByFeature(lastFeature);
				myModelSoft->m_historyManager->redoByFeature(lastFeature);
				int row1 = myModelSoft->m_historyManager->historyTree->rowCount() - 1;
				myModelSoft->m_historyManager->historyTree->removeRow(row1);
				modify = true;
				ui->confirmButton->setEnabled(true);
			}
		}
		else {
			if (featureModify) {
				//还原特征值
				updateFeature(selectedFeature,lastExpressionValue);
			}
		}
	}
}

void expressionWidget::onConfirm()
{
	confirm = true;
	close();
}
